
//  节流
const throttle = (fn, time) => {
    let flag = true
    return function () {
        if (flag == false) return
        flag = false
        setTimeout(() => {
            fn.apply(this)
            flag = true
        }, time)
    }
}
// logo切换效果
function logoChange() {
    let logoState = false
    $('#head').on('mouseover', '>.left', function (e) {
        target = e.target || e.srcElement
        if (!logoState) {
            logoState = true
            $(target).css({
                background: 'url(../static/logo_1.gif) no-repeat 0 0/190px 120px'
            })
            setTimeout(function () {
                $(target).css('background', 'url(../static/logo.png) no-repeat')
                logoState = false
            }, 6000)
        }
    })
}
logoChange()
// 轮播
// 获取轮播商品信息
async function getData() {
    let data = await myPromise({
        method: 'get',
        url: 'http://api.yuguoxy.com/api/shop/banner'
    })
    let { resultCode, resultInfo } = data
    if (resultCode == 1) {
        banner(resultInfo.list)
    }
}
// 实现轮播效果

// 动态渲染页面
function banner(data) {
    let bannerData = data

    let str = `<div class="swiper-wrapper">`
    let strArr = bannerData.map(item => {
        return `<div class="swiper-slide">
               <img src="${item.url}" alt="">
               </div>`
    })

    str = str + strArr.join('') + `</div>
            <div class="swiper-pagination"></div>`

    $('main #bannerAndOther .banner .swiper').html(str)
    new Swiper('.swiper', {
        loop: true, // 循环模式选项
        autoplay: true,
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
            clickable: true,
        },
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
    })

}
getData()


// 动态渲染商品列表--为你推荐
let productListAll = []
//  1、获取商品数据
async function getProductList(page, num) {
    let pageSize = page || 10
    let pageNo = num || 1
    let data = await myPromise({
        method: 'get',
        url: `http://api.yuguoxy.com/api/shop/list?pageSize=${pageSize}&pageNo=${pageNo}`
    })
    let { resultCode, resultInfo } = data
    if (resultCode == 1) {
        productList(resultInfo.list)
    }
}
getProductList()
// 2、动态渲染商品数据
function productList(data) {
    productListAll = [...productListAll, ...data]
    let productArr = productListAll.map(item => {
        return `<a class="productItem" href="#" index="${item.id}">
                    <img src="${item.picture}" alt=""> 
                    <p>${item.shop}</p> 
                    <div>￥${(item.price).toFixed(2)}</div>
                </a>`
    })
    let productStr = productArr.join('')
    $('#forYou .content').html(productStr)
    getDataMore()

}
// 3、点击按钮再次加载商品数据
function getDataMore() {
    // 获取为你推荐模块，距离页面顶部的距离
    let n = 1
    $('main #add').on('click', 'button', function () {
        n++
        if (n < 3) {
            getProductList(10, n)
        } else {
            $('main #add').html('已经到底拉！')
        }
    })

}

// 点击商品跳转详情页
function productDetail() {
    $('#forYou .content').on('click', 'a', function (e) {
        target = e.target || e.srcElement
        e.preventDefault()
        let id = $(this).attr('index')
        location.href = `./product.html?id=${id}`
    })
}
productDetail()




// document.addEventListener('mousewheel', mouseWheel=function (e) {
//     html_y = parseInt(($(document).scrollTop()))
//     state = html_y > (y + (2*n * box_height)) ? true : false
//     console.log(state)
//     e = e || window.event
//     if (e.deltaY > 0 && state) {
//         n++
//         if (n > 2) {
//             console.log(n)
//             document. removeEventListener('mousewheel',mouseWheel())
//         } else {
//             getProductList(10, n)
//             console.log(n)

//         }
//     }

// })


